﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.ComponentModel.DataAnnotations;
using System.Data.Entity;
using System.Web.Mvc;
using System.ComponentModel;
using Uco.Infrastructure;
using Telerik.Web.Mvc.UI;
using System.Xml.Serialization;
using System.IO;

namespace Uco.Models
{

    public abstract class AbstractPage
    {
        [HiddenInput(DisplayValue = false)]
        [Key]
        public int ID { get; set; }
        [Display(Name = "Title", Order = 100, ResourceType = typeof(Uco.Content.Resources.SystemModels), Prompt = "TabContent")]
        [Required(ErrorMessageResourceType = typeof(Uco.Content.Resources.SystemModels), ErrorMessageResourceName = "TitleRequired")]
        public string Title { get; set; }
        [Display(Name = "Text", Order = 150, ResourceType = typeof(Uco.Content.Resources.SystemModels), Prompt = "TabContent")]
        [UIHint("Html")]
        [AllowHtml]
        public string Text { get; set; }

        [Display(Name = "Link", Order = 200, ResourceType = typeof(Uco.Content.Resources.SystemModels), Prompt = "TabSettings")]
        [UIHint("Link")]
        public string Link { get { return this.Url; } }
        [Display(Name = "Layout", Order = 200, ResourceType = typeof(Uco.Content.Resources.SystemModels), Prompt = "TabSettings")]
        [UIHint("Layouts")]
        public virtual string Layout { get; set; }
        [Display(Name = "Order", Order = 210, ResourceType = typeof(Uco.Content.Resources.SystemModels), Prompt = "TabSettings")]
        public int Order { get; set; }
        [Display(Name = "Visible", Order = 220, ResourceType = typeof(Uco.Content.Resources.SystemModels), Prompt = "TabSettings")]
        public bool Visible { get; set; }
        [Display(Name = "ShowInMenu", Order = 230, ResourceType = typeof(Uco.Content.Resources.SystemModels), Prompt = "TabSettings")]
        public bool ShowInMenu { get; set; }
        [Display(Name = "ShowInSitemap", Order = 240, ResourceType = typeof(Uco.Content.Resources.SystemModels), Prompt = "TabSettings")]
        public bool ShowInSitemap { get; set; }
        [Display(Name = "ShowChildrensAsTableOnly", Order = 250, ResourceType = typeof(Uco.Content.Resources.SystemModels), Prompt = "TabSettings")]
        public bool ShowChildrensAsTableOnly { get; set; }
        [Display(Name = "CreateTime", Order = 260, ResourceType = typeof(Uco.Content.Resources.SystemModels), Prompt = "TabSettings")]
        [UIHint("DateTime")]
        public DateTime CreateTime { get; set; }
        [Display(Name = "ChangeTime", Order = 270, ResourceType = typeof(Uco.Content.Resources.SystemModels), Prompt = "TabSettings")]
        [UIHint("DateTime")]
        public DateTime ChangeTime { get; set; }

        [Display(Name = "SeoUrlName", Order = 300, ResourceType = typeof(Uco.Content.Resources.SystemModels), Prompt = "TabSeo")]
        [RegularExpression("^[a-zA-Zא-ת0-9_]+$", ErrorMessageResourceType = typeof(Uco.Content.Resources.SystemModels), ErrorMessageResourceName = "SeoUrlNameError")]
        public string SeoUrlName { get; set; }
        [Display(Name = "SeoTitle", Order = 310, ResourceType = typeof(Uco.Content.Resources.SystemModels), Prompt = "TabSeo")]
        public string SeoTitle { get; set; }
        [Display(Name = "SeoH1", Order = 320, ResourceType = typeof(Uco.Content.Resources.SystemModels), Prompt = "TabSeo")]
        public string SeoH1 { get; set; }
        [Display(Name = "SeoInLinkName", Order = 330, ResourceType = typeof(Uco.Content.Resources.SystemModels), Prompt = "TabSeo")]
        public string SeoInLinkName { get; set; }
        [Display(Name = "SeoKywords", Order = 340, ResourceType = typeof(Uco.Content.Resources.SystemModels), Prompt = "TabSeo")]
        [DataType(DataType.MultilineText)]
        public string SeoKywords { get; set; }
        [Display(Name = "SeoDescription", Order = 340, ResourceType = typeof(Uco.Content.Resources.SystemModels), Prompt = "TabSeo")]
        [DataType(DataType.MultilineText)]
        public string SeoDescription { get; set; }

        [Display(Name = "ParentID", ResourceType = typeof(Uco.Content.Resources.SystemModels))]
        [HiddenInput(DisplayValue = false)]
        public int? ParentID { get; set; }

        [HiddenInput(DisplayValue = false)]
        public virtual string RouteUrl { get; set; }

        [HiddenInput(DisplayValue = false)]
        public string Url { get { return "~/" + this.RouteUrl + "/" + this.SeoUrlName; }}

        [Display(Prompt = "Hidden")]
        public virtual string ShortDescription { get; set; }
		[Display(Prompt = "Hidden")]
        public virtual string Pic { get; set; }

        public AbstractPage()
        {
            Order = 100;
            Layout = "~/Views/Shared/_Layout.cshtml";
            Visible = true;
            ShowInMenu = true;
            ShowInSitemap = true;
            ShowChildrensAsTableOnly = false;
            CreateTime = DateTime.Now;
            ChangeTime = DateTime.Now;
        }


        public virtual void Save()
        {
            Type[] t = SF.GetAbstractPageChildClasses().ToArray();
            XmlSerializer AbstractPageSerializer = new XmlSerializer(typeof(AbstractPage), t);
            StreamWriter AbstractWriter = new StreamWriter(HttpContext.Current.Server.MapPath("~/App_Data/pages/" + this.SeoUrlName + ".xml"));
            AbstractPageSerializer.Serialize(AbstractWriter, this);
            AbstractWriter.Close();
            AbstractWriter.Dispose();
        }

        public virtual void Delete()
        {
            File.Delete(HttpContext.Current.Server.MapPath("~/App_Data/pages/" + this.SeoUrlName + ".xml"));
        }
    }

    [RestrictParents(new string[] { "LanguagePage" })]
    [RouteUrl("l")]
    public class LanguagePage : AbstractPage
    {
        [HiddenInput(DisplayValue = false)]
        public override string RouteUrl { get { return "l"; } set { } }

        public LanguagePage()
        {

        }
    }

    [RestrictParents(new string[] { "LanguagePage" })]
    [RouteUrl("sm")]
    public class SiteMapPage : AbstractPage
    {
        [HiddenInput(DisplayValue = false)]
        public override string RouteUrl { get { return "sm"; } set { } }
    }

    [RestrictParents(new string[] { "LanguagePage" })]
    [RouteUrl("arl")]
    public class ArticleAll : AbstractPage
    {
        [HiddenInput(DisplayValue = false)]
        public override string RouteUrl { get { return "arl"; } set { } }
    }

    [RestrictParents(new string[] { "ArticleAll" })]
    [RouteUrl("ar")]
    public class Article : AbstractPage
    {
        [Display(Name = "ShortDescription", Order = 105, ResourceType = typeof(Uco.Content.Resources.SystemModels), Prompt = "TabContent")]
        [DataType(DataType.MultilineText)]
        public override string ShortDescription { get; set; }

        [HiddenInput(DisplayValue = false)]
        public override string RouteUrl { get { return "ar"; } set { } }
    }

    [RestrictParents(new string[] { "LanguagePage", "ContentPage" })]
    [RouteUrl("f")]
    public class Form : AbstractPage
    {
        [Display(Name = "FormTextAfter", Order = 150, Prompt = "TabContent", ResourceType = typeof(Uco.Content.Resources.SystemModels))]
        [UIHint("Html")]
        [AllowHtml]
        public string FormTextAfter { get; set; }

        [Display(Name = "כתובת", Order = 105, Prompt = "מפה")]
        [UIHint("Map")]
        public override string Pic { get; set; }

        [HiddenInput(DisplayValue = false)]
        public override string RouteUrl { get { return "f"; } set { } }
    }

    [RestrictParents(new string[] { "LanguagePage" })]
    [RouteUrl("g")]
    public class GalleryAll : AbstractPage
    {
        [Display(Name = "Pictures", Order = 400, Prompt = "TabData", ResourceType = typeof(Uco.Content.Resources.SystemModels))]
        [UIHint("Gallery")]
        public string Gallery { get { return "Gallery/gallery_" + this.ID; } }

        [HiddenInput(DisplayValue = false)]
        public override string RouteUrl { get { return "g"; } set { } }
    }

    [RestrictParents(new string[] { "LanguagePage" })]
    [RouteUrl("nl")]
    public class NewsAllPage : AbstractPage
    {
        [HiddenInput(DisplayValue = false)]
        public override string RouteUrl { get { return "nl"; } set { } }
    }

    [RestrictParents(new string[] { "NewsAllPage" })]
    [RouteUrl("n")]
    public class NewsPage : AbstractPage
    {
        [Display(Name = "ShortDescription", Order = 105, ResourceType = typeof(Uco.Content.Resources.SystemModels), Prompt = "TabContent")]
        [DataType(DataType.MultilineText)]
        public override string ShortDescription { get; set; }

        [HiddenInput(DisplayValue = false)]
        public override string RouteUrl { get { return "n"; } set { } }
    }

    [RestrictParents(new string[] { "LanguagePage" })]
    [RouteUrl("r")]
    public class RedirectPage : AbstractPage
    {
        [Display(Name = "RedirectTo", Order = 104, ResourceType = typeof(Uco.Content.Resources.SystemModels), Prompt = "Content")]
        [UIHint("Url")]
        public string RedirectTo { get; set; }

        [HiddenInput(DisplayValue = false)]
        public override string RouteUrl { get { return "r"; } set { } }
    }

    [RestrictParents(new string[] { "LanguagePage", "ContentPage" })]
    [RouteUrl("c")]
    public class ContentPage : AbstractPage
    {
        [HiddenInput(DisplayValue = false)]
        public override string RouteUrl { get { return "c"; } set { } }
    }

}